# Maintainer: Anatol Pomozov
# Maintainer: Iru Cai <mytbk920423@gmail.com>

pkgname=coreboot-utils
pkgver=4.16
pkgrel=1
pkgdesc='Tools and utilities to work with coreboot firmware'
url='https://www.coreboot.org/'
license=(GPL)
arch=(x86_64)
depends=(pciutils)
optdepends=("python: me_cleaner support")
# for latest code, run:
# git diff 4.12 util/cbfstool/ util/ifdtool/ util/nvramtool/ util/inteltool/ util/intelmetool/ util/superiotool/ util/cbmem/ util/romcc/ util/ectool/ util/intelvbttool/ util/autoport/ util/me_cleaner/
source=("https://coreboot.org/releases/coreboot-${pkgver}.tar.xz"
        autoport-tool-paths.patch)
sha256sums=('95e2fb4d51b923f3b5f92953f2501b25a3ee6f86cd5572a5ce1aeab3c0fc932d'
            '5136f8ae6c690501b483243a33b91dc978971cf23bcf48c27bdca845db53bdb1')

HAS_GOLANG=y

if [ "${HAS_GOLANG}" == y ]; then
	makedepends=("${makedepends[@]}" go)
fi

prepare() {
	cd "${srcdir}/coreboot-${pkgver}"
	#patch -p1 -i "$srcdir/coreboot-utils-20201005.patch"
	patch -p1 -i "$srcdir/autoport-tool-paths.patch"
	#cd 3rdparty/vboot
	#patch -p1 -i "$srcdir/vboot_latest.patch"
}

build() {
  cd "${srcdir}/coreboot-${pkgver}/util"

  make -C ifdtool
  make -C cbfstool
  make -C nvramtool
  make -C inteltool
  make -C intelmetool
  make -C superiotool
  make -C cbmem
  make -C ectool
  make -C intelvbttool
  if [ "$HAS_GOLANG" == y ]; then
    cd autoport
    go build -trimpath
    cd ..
    make -C intelp2m
  fi
}

package() {
  cd "${srcdir}/coreboot-${pkgver}/util"
  install -m755 -d "$pkgdir/usr/bin" "$pkgdir/usr/share/man/man8"
  install -m755 -t "$pkgdir/usr/bin" ifdtool/ifdtool nvramtool/nvramtool inteltool/inteltool superiotool/superiotool cbmem/cbmem ectool/ectool intelmetool/intelmetool intelvbttool/intelvbttool cbfstool/cbfstool
  install -m755 "me_cleaner/me_cleaner.py" "$pkgdir/usr/bin/me_cleaner"
  if [ "${HAS_GOLANG}" == y ]; then
	  install -m755 -t "$pkgdir/usr/bin" autoport/autoport intelp2m/intelp2m
  fi
  install -m644 -t "$pkgdir"/usr/share/man/man8 inteltool/inteltool.8

  install -Dm644 ../COPYING "$pkgdir/usr/share/licenses/$pkgname/COPYING"
}
